﻿asteroid.prototype.logic = function() {
	var s2 = this.size / 2;
	this.x += this.vx;
	this.y += this.vy;
	this.angle += this.rotation;
	
	for(var i = ships.length; i--; ) {
		if(collision(this.x, this.y, this.size, this.size, ships[i].x, ships[i].y, 20, 20)) {
			var damage = parseInt(this.size);
			infoTexts.push(new infoText(this.x, this.y, 50, '-' + damage, secondaryColors[ships[i].color]));
			explosions.push(new explosion(this.x, this.y, this.size, implosionImg));
			ships[i].hitByAsteroid += 1;
			ships[i].life -= damage;
			return false;
		}
	}
	
	if(this.x + s2 < 0)
		this.x = c.canvas.width + s2;
	else if(this.x - s2 > c.canvas.width)
		this.x = -s2;
		
	if(this.y + s2 < 0)
		this.y = c.canvas.height + s2;
	else if(this.y - s2 > c.canvas.height)
		this.y = -s2;
	
	return true;
};

particle.prototype.logic = function() {
	this.x += this.vx;
	this.y += this.vy;
	
	for(var i = asteroids.length; i--; ) {
		if(collision(this.x, this.y, 3, 3, asteroids[i].x, asteroids[i].y, asteroids[i].size, asteroids[i].size)) {
			var damage = parseInt(DAMAGE_FACTOR * this.lifetime / MAX_PARTICLE_TIME);
			this.owner.hitAsteroids += 1;
			asteroids[i].life -= damage;
			
			if(asteroids[i].life <= 0) {
				infoTexts.push(new infoText(asteroids[i].x, asteroids[i].y, 50, '+1', primaryColors[this.owner.color]));
				explosions.push(new explosion(asteroids[i].x, asteroids[i].y, asteroids[i].size));
				this.owner.shotAsteroids += 1;
				asteroids.splice(i, 1);
			} else
				infoTexts.push(new infoText(asteroids[i].x, asteroids[i].y, 50, '(' + damage + ')', primaryColors[this.owner.color]));
				
			return false;
		}		
	}
	
	for(var i = ships.length; i--; ) {
		if(this.owner.id !== ships[i].id && collision(this.x, this.y, 3, 3, ships[i].x, ships[i].y, 20, 20)) {
			var damage = parseInt(DAMAGE_FACTOR * this.lifetime / MAX_PARTICLE_TIME);
			infoTexts.push(new infoText(this.x, this.y, 50, '-' + damage, primaryColors[ships[i].color]));
			this.owner.hitShips += 1;
			ships[i].life -= damage;
			return false;
		}		
	}
	
	return this.lifetime--;
};

explosion.prototype.logic = function() {
	return this.frame--;
};

infoText.prototype.logic = function() {
	return this.time--;
};

ship.prototype.logic = function() {
	if(this.control.left)
		this.angle -= 4;
		
	if(this.control.right)
		this.angle += 4;
		
	if(this.control.up) {
		this.boost += ACCELERATE;
		
		if(this.boost > MAX_SPEED)
			this.boost = MAX_SPEED;
	}
	
	if(this.control.down) {
		this.boost -= 0.1;
		
		if(this.boost < 0)
			this.boost = 0;
	}
	
	this.y -= this.boost * Math.cos(d2g(this.angle));
	this.x += this.boost * Math.sin(d2g(this.angle));
	
	if(this.y + 12 < 0)
		this.y = c.canvas.height + 12;
	else if(this.y - 12 > c.canvas.height)
		this.y = -12;
	
	if(this.x + 10 < 0)
		this.x = c.canvas.width + 10;
	else if(this.x - 10 > c.canvas.width)
		this.x = -10;
	
	if(this.cooldown) {
		--this.cooldown;
	}
	else if(this.control.shoot && this.ammo > 0) {
		--this.ammo;
		this.cooldown = INIT_COOLDOWN;
		particles.push(new particle(this.x, this.y, (4 + this.boost) * Math.sin(d2g(this.angle)), - (4 + this.boost) * Math.cos(d2g(this.angle)), this));
	}
};

var logic = function() {
	if(multiPlayer) {
		socket.send(JSON.stringify({ keyboard : keyboard, ship : getShip(1) }));
	}
	
	var i;
		
	for(i = explosions.length; i--; )
		if(!explosions[i].logic())
			explosions.splice(i, 1);
		
	for(i = infoTexts.length; i--; )
		if(!infoTexts[i].logic())
			infoTexts.splice(i, 1);
	
	for(i = ships.length; i--; )
		ships[i].logic();
		
	for(i = particles.length; i--; ) {
		if(!particles[i].logic())
			particles.splice(i, 1);
	}
		
	for(i = asteroids.length; i--; )
		if(!asteroids[i].logic())
			asteroids.splice(i, 1);
			
	for(i = ships.length; i--; )
		if(ships[i].life <= 0) {
			explosions.push(new explosion(ships[i].x, ships[i].y, 24, shiposionImg));
			deadVessels.push(ships[i]);
			
			if(ships[i].id === 1) {
				//TODO Score Screen einblenden!
			}
			
			ships.splice(i, 1);
		}
};

var items = function() {
	if(multiPlayer)
		return;
		
	if(ticks % 100 === 0)
		asteroids.push(generateAsteroid());
};